
package com.eeprint.web.controller.tenant;



import com.eeprint.common.utils.AjaxResult;
import com.eeprint.common.utils.TokenUtil;
import com.eeprint.system.domain.platform.FORM.PlatformUserUpdateFORM;
import com.eeprint.system.domain.tenant.TenantUser;
import com.eeprint.system.domain.tenant.form.TenantUserUpdate;
import com.eeprint.system.service.tenant.TenantUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TenantUserController {

    @Autowired
    TenantUserService tenantUserService;

    /**
     * @Author: wsen on 2020/10/19 17:33
     * @Description: 管理员登录
     */

    //TODO 密码的加密 统一返回 Token的生成 数据校验
    @PostMapping("/enterprise/open/user/login")
    public AjaxResult login(@RequestBody @Valid TenantUser tenantUser){
        TenantUser user = tenantUserService.checkUser(tenantUser.getUserPhone(),tenantUser.getUserPassword());
        if(user != null){
            String token = TokenUtil.createToken(user.getId().toString());
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            map.put("id",user.getId());
            map.put("relTenantId",user.getRelTenantId());
            map.put("userNick",user.getUserNick());
            map.put("userLogo",user.getUserLogo());
            return AjaxResult.success(map);
        }
        return AjaxResult.resourcesError("手机号码或密码错误");
    }

    /**
      * @Author newhonor
      * @Date 2020/10/19
      * @Version 1.0
    **/

    @PostMapping("/tenant/auth/user/list")
    public AjaxResult findTenantUsers(@RequestBody Map<String,Object> map){
        BigInteger id = new BigInteger(map.get("relTenantId").toString());
        List<TenantUser> list = tenantUserService.findTenantUsersByTenantId(id,(int)map.get("page"),(int)map.get("pageSize"));
        Map<String,Object> data = new HashMap<>();

        int totalCount = tenantUserService.countTenantUserByTenantId(id);
        int pageSize = (int)map.get("pageSize");
        data.put("totalCount",totalCount);
        data.put("pageSize",pageSize);
        int pages = totalCount/pageSize;
        if(totalCount%pageSize>0) pages++;
        data.put("totalPage",pages);
        data.put("curPage",list);

        AjaxResult result=null;
        if(list!=null){
            result = AjaxResult.success(data);
        }else {
            result=AjaxResult.error();
        }
        return result;
    }

    @PostMapping("/tenant/auth/user/add")
    public AjaxResult addTenantUser(@RequestBody TenantUser tenantUser){
        AjaxResult result=null;
        if(tenantUserService.addTenantUser(tenantUser)){
            result = AjaxResult.success();
        }else {
            result = AjaxResult.error();
        }
        return result;
    }

    @PostMapping("/tenant/auth/user/edit")
    public AjaxResult updateTenantUser(@RequestBody TenantUser tenantUser){
        AjaxResult result = null;
        if(tenantUserService.updateTenantUser(tenantUser)){
            result=AjaxResult.success();
        }else {
            result=AjaxResult.error();
        }
        return result;
    }

    @PostMapping("/tenant/auth/user/editPsw")
    public AjaxResult updateTenantUserPassword(@RequestBody TenantUserUpdate tenantUserUpdate){

        AjaxResult result=null;
        if(tenantUserService.updateTenantUserPassword(tenantUserUpdate)){
            result = AjaxResult.success();
        }else {
            result=AjaxResult.error();
        }
        return result;
    }

}
